package com.fjh.springboot_01.controller;

import com.fjh.springboot_01.pojo.homework.Homework;
import com.fjh.springboot_01.pojo.homework.HomeworkQuestion;
import com.fjh.springboot_01.pojo.homework.HomeworkAnswer;
import com.fjh.springboot_01.pojo.Result;
import com.fjh.springboot_01.service.HomeworkService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/homework")
public class HomeworkController {
    @Autowired
    private HomeworkService homeworkService;

    // 教师创建作业
    @PostMapping
    public Result<Void> createHomework(@RequestBody Map<String, Object> requestBody) {
        Homework homework = new ObjectMapper().convertValue(requestBody.get("homework"), Homework.class);
        List<HomeworkQuestion> questions = new ObjectMapper().convertValue(requestBody.get("questions"), 
            new TypeReference<List<HomeworkQuestion>>() {});
        homeworkService.createHomework(homework, questions);
        return Result.success();
    }

    // 获取作业详情
    @GetMapping("/{id}")
    public Result<Homework> getHomework(@PathVariable Integer id) {
        return Result.success(homeworkService.getHomeworkById(id));
    }

    // 获取教师发布的作业列表
    @GetMapping("/teacher/{teacherId}")
    public Result<List<Homework>> getTeacherHomeworks(@PathVariable Integer teacherId) {
        return Result.success(homeworkService.getTeacherHomeworks(teacherId));
    }

    // 获取学生的作业列表
    @GetMapping("/student/{studentId}")
    public Result<List<Homework>> getStudentHomeworks(@PathVariable Integer studentId) {
        return Result.success(homeworkService.getStudentHomeworks(studentId));
    }

    // 更新作业状态
    @PutMapping("/{id}/status")
    public Result<Void> updateHomeworkStatus(@PathVariable Integer id,
                                           @RequestParam Integer status) {
        homeworkService.updateHomeworkStatus(id, status);
        return Result.success();
    }

    // 添加作业题目
    @PostMapping("/question")
    public Result<Void> addQuestion(@RequestBody HomeworkQuestion question) {
        homeworkService.addQuestion(question);
        return Result.success();
    }

    // 更新作业题目
    @PutMapping("/question")
    public Result<Void> updateQuestion(@RequestBody HomeworkQuestion question) {
        homeworkService.updateQuestion(question);
        return Result.success();
    }

    // 删除作业题目
    @DeleteMapping("/question/{questionId}")
    public Result<Void> deleteQuestion(@PathVariable Integer questionId) {
        homeworkService.deleteQuestion(questionId);
        return Result.success();
    }

    // 获取作业题目列表
    @GetMapping("/{homeworkId}/questions")
    public Result<List<HomeworkQuestion>> getQuestions(@PathVariable Integer homeworkId) {
        return Result.success(homeworkService.getQuestionsByHomeworkId(homeworkId));
    }

    // 提交作业答案
    @PostMapping("/answer")
    public Result<Void> submitAnswer(@RequestBody HomeworkAnswer answer) {
        homeworkService.submitAnswer(answer);
        return Result.success();
    }

    // 更新作业答案
    @PutMapping("/answer/{answerId}")
    public Result<Void> updateAnswer(@PathVariable Integer answerId,
                                   @RequestParam String answerContent) {
        homeworkService.updateAnswer(answerId, answerContent);
        return Result.success();
    }

    // 获取学生的作业答案
    @GetMapping("/{homeworkId}/student/{studentId}/answers")
    public Result<List<HomeworkAnswer>> getStudentAnswers(@PathVariable Integer homeworkId,
                                                        @PathVariable Integer studentId) {
        return Result.success(homeworkService.getAnswersByHomeworkAndStudent(homeworkId, studentId));
    }

    // 教师批改作业
    @PutMapping("/answer/{answerId}/grade")
    public Result<Void> gradeAnswer(@PathVariable Integer answerId,
                                  @RequestParam String teacherComment,
                                  @RequestParam Integer score) {
        homeworkService.gradeAnswer(answerId, teacherComment, score);
        return Result.success();
    }

    // 自动批改单个答案
    @PutMapping("/answer/{answerId}/auto-grade")
    public Result<Void> autoGradeAnswer(@PathVariable Integer answerId) {
        homeworkService.autoGradeAnswer(answerId);
        return Result.success();
    }

    // 自动批改整份作业
    @PutMapping("/{homeworkId}/student/{studentId}/auto-grade")
    public Result<Void> autoGradeHomework(@PathVariable Integer homeworkId,
                                        @PathVariable Integer studentId) {
        homeworkService.autoGradeHomework(homeworkId, studentId);
        return Result.success();
    }

    // AI出题相关接口
    @PostMapping("/ai/generate")
    public Result<List<HomeworkQuestion>> generateQuestionsByAI(
            @RequestParam Integer categoryId,
            @RequestParam Integer knowledgePointId,
            @RequestParam Integer questionType,
            @RequestParam(defaultValue = "1") Integer questionCount,
            @RequestParam(required = false) String topic) {
        return Result.success(
            homeworkService.generateQuestionsByAI(categoryId, knowledgePointId, questionType, questionCount, topic)
        );
    }

    @PostMapping("/ai/question")
    public Result<String> generateQuestionContent(
            @RequestParam String topic,
            @RequestParam Integer questionType) {
        return Result.success(
            homeworkService.generateQuestionContent(topic, questionType)
        );
    }

    @PostMapping("/ai/answer")
    public Result<String> generateStandardAnswer(
            @RequestParam String questionContent,
            @RequestParam Integer questionType) {
        return Result.success(
            homeworkService.generateStandardAnswer(questionContent, questionType)
        );
    }

    @PostMapping("/ai/options")
    public Result<List<String>> generateOptions(
            @RequestParam String questionContent,
            @RequestParam String standardAnswer) {
        return Result.success(
            homeworkService.generateOptions(questionContent, standardAnswer)
        );
    }

    // 创建错题记录
    // ... existing code ...
} 